package com.naihe.anderson.sms.dbhelper;

public class DbReadWriteLock {
    private boolean isRead;
    private boolean isWrite;
    public synchronized void readLock(){
        while(isWrite){
            try{    
                wait();
            }
            catch(InterruptedException ex){
                ex.printStackTrace();
            }
        }
        
        isRead=true;
    }
    
    public synchronized void readUnlock(){
        isRead=false;
        notifyAll();
    }

     public synchronized void writeLock(){
        while(isRead){
            try{    
                wait();
            }
            catch(InterruptedException ex){
                ex.printStackTrace();
            }
        }
        
        while(isWrite){
            try{    
                wait();
            }
            catch(InterruptedException ex){
                ex.printStackTrace();
            }
        }
        
        isWrite=true;
    }
    
    public synchronized void writeUnlock(){
        isWrite=false;
        notifyAll();
    }


}
